package com.lihao.sam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lihao.sam.dao.ApartmentChaNormDao;
import com.lihao.sam.dto.ApartUnitDto;
import com.lihao.sam.entity.ApartmentChaNorm;
import com.lihao.sam.service.intf.ApartmentChaNormService;
import com.lihao.sam.vo.PageVo;
import com.lihao.sam.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * *  作者:陈国强
 * *  日期:2022/11/22 17:04
 * *  文件描述:
 */
@Service
public class ApartmentChaNormServiceImpl implements ApartmentChaNormService {
    @Resource
    private ApartmentChaNormDao dao;

    @Override
    public R add(ApartmentChaNorm chaNorm) {
        //1.校验参数
        if(chaNorm!=null){
            //2.操作数据库
            if(dao.add(chaNorm)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R update(ApartmentChaNorm chaNorm) {
        //1.校验参数
        if(chaNorm!=null){
            //2.操作数据库
            if(dao.update(chaNorm)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R delete(int id) {
        //1.校验参数
        if(id>0){
            //2.操作数据库
            if(dao.delete(id)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    //批量删除
    @Override
    public R delBath(int[] array) {
        if (dao.delBatch(array) > 0) {
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public PageVo queryPage(String chargeNorm, String unit, Integer bedNumber, int page, int limit) {
        //1.开启分页
        PageHelper.startPage(page,limit);
        //2.开始查询
        PageInfo<ApartmentChaNorm> pageInfo=new PageInfo<>(dao.selectByWhere(chargeNorm, unit,bedNumber));
        //3.返回对应的结果
        return PageVo.ok(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public PageVo selectAll(int page, int limit) {
        //
        PageHelper.startPage(page, limit);
        PageInfo<ApartmentChaNorm> pageInfo =new PageInfo<>(dao.selectAll());
        //3.返回
        return PageVo.ok(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R importData(MultipartFile file) {
        //1.获取文件上传的内容
        if(!file.isEmpty()){
            try {
                InputStream stream = file.getInputStream();
                //2.处理文件内容，得到集合
                BufferedReader buf = new BufferedReader(new InputStreamReader(stream));
                List<ApartmentChaNorm> list = new ArrayList<>();
                String r = null;
                while ((r= buf.readLine())!=null){
                    //分割  读取到这一行的数据
                    String[] arr = r.split(" ");
                    list.add(new ApartmentChaNorm(arr[0],arr[1],Integer.parseInt(arr[2])));
                }
                buf.close();
                //3.实现批量新增
                if (dao.addBatch(list)>0){
                    return R.ok();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }

    @Override
    public void exportData(HttpServletResponse response) throws IOException {
//1.设置下载的文件名
        String s1 = "公寓收费标准信息-"+System.currentTimeMillis()+".txt";
        //2.设置下载的响应头消息  固定
        response.setHeader("content-disposition","attachment;filename="+s1);
        response.setCharacterEncoding("UTF-8");
        //3.查询下载的数据
        List<ApartmentChaNorm> list = dao.selectAll();
        //4.IO流写出
        PrintWriter writer = response.getWriter();
        for (ApartmentChaNorm norm : list) {
            writer.print(norm.toString());
        }
        writer.flush();
    }

    @Override
    public R sekectUnit() {
        //1.查询
        List<ApartUnitDto> list = dao.selectUnit();
        //2.组装结果数据
        Map<String,ArrayList> map = new HashMap<>();
        map.put("unit",new ArrayList<>());
        map.put("num",new ArrayList<>());
        for (ApartUnitDto au : list) {
            map.get("unit").add(au.getUnit());
            map.get("num").add(au.getNum());
        }
        return R.ok(map);
    }
}
